<?php
class CourseRecordController {
	public function add(){
		if (!empty($_GET['bookCourseId'])){
			$courseRecord = HttpDaoManager::getCourseRecordDao()->getByBookCourseId($_GET['bookCourseId']);
			$ret_data =HttpServerManager::getBookCourseServer()->bookCourseDetailInfo($_GET['bookCourseId']);
			$user = HttpDaoManager::getUserDao()->get($courseRecord['user_id']);
			$level_list = HttpDaoManager::getLevelDao()->searchList();
			$level_pair = array();
			foreach($level_list as $k=>$v){
				if(isset($v['id'])){
					$level_pair[$v['id']]='Level'.'-'.$v['level'];
				}			
			}	
			
			$courseRecord['userLevelOption'] = page_modifier_options_ext($level_pair,$user['level']);
			$int_book_time = strtotime(substr($ret_data['book']['book_time'],0,strripos($ret_data['book']['book_time'],'-')).':00');
			$ret_data['courseRecord'] = $courseRecord;
			$ret_data['book']['int_book_time'] = $int_book_time;
			$userfilter = array();
			$userfilter['user_id'] = $ret_data['book']['user_id'];
			$ret_data['userData'] = HttpServerManager::getUserServer()->getUserInfo($userfilter);
			$ret_data['user_data']['user_kind'] = Common::_t('user_kind');
		}
		return $ret_data;
	}
	public function recordInfo(){
		$ret_data = array();
		if (!empty($_GET['bookCourseId'])){
			$courseRecord = HttpDaoManager::getCourseRecordDao()->getByBookCourseId($_GET['bookCourseId']);
			$bookData = HttpDaoManager::getBookCourseDao()->get($_GET['bookCourseId']);
			$user = HttpDaoManager::getUserDao()->get($bookData['user_id']);
			$level_list = HttpDaoManager::getLevelDao()->searchList();
			$level_pair = array();
			foreach($level_list as $k=>$v){
				if(isset($v['id'])){
					$level_pair[$v['id']]='Level'.'-'.$v['level'];
				}
			}
			$courseRecord['user_level'] = $level_pair[$user['level']];		
			$courseRecord['book_type']=$bookData['book_type'];
			$courseRecord['user_status'] = $bookData['user_status'];
			
			$userStatus = Common::_t('book_user_status');
			$courseRecord['user_status_list'] = $userStatus;
			
			$ret_data =HttpServerManager::getBookCourseServer()->bookCourseDetailInfo($_GET['bookCourseId']);
			$ret_data['courseRecord'] = $courseRecord;
			$userfilter = array();
			$userfilter['user_id'] = $bookData['user_id'];
			$ret_data['userData'] = HttpServerManager::getUserServer()->getUserInfo($userfilter);		
			$ret_data['user_data']['user_kind'] = Common::_t('user_kind');
		}
		return $ret_data;
	}

	public function subAdd(){
		$data = $_POST;
		HttpServerManager::getCourseRecordServer()->add($data);
		
		$courseRecord['user_status'] = $_POST['user_status'];
		HttpServerManager::getBookCourseServer()->haveClass($courseRecord,$_POST['book_course_id']);
		ClassManager::getToos()->msgbox(Common::_t('data_insert_success'));
	}
	
	public function subUpdate(){
		HttpDaoManager::getCourseRecordDao()->update($_POST, $_POST['id']);
		
		if(isset($_POST['user_level'])&&$_POST['user_level']>0){
			$userData = array();
			$userData['level'] = $_POST['user_level'];
			$user_id = $_POST['user_id'];
			HttpDaoManager::getUserDao()->update($userData,$user_id);	
		}
		$courseRecord['user_status'] = $_POST['user_status'];
		HttpServerManager::getBookCourseServer()->haveClass($courseRecord,$_POST['book_course_id']);
		ClassManager::getToos()->msgbox(Common::_t('data_edit_success'));
	}
	
	public function subComplete(){
		if(isset($_POST['id'])&&$_POST['id']>0){
			HttpDaoManager::getCourseRecordDao()->update($_POST, $_POST['id']);
			if(isset($_POST['user_level'])&&$_POST['user_level']>0){
				$userData = array();
				$userData['level'] = $_POST['user_level'];
				$user_id = $_POST['user_id'];
				HttpDaoManager::getUserDao()->update($userData,$user_id);	
			}
		}else{
			$data = $_POST;
			HttpServerManager::getCourseRecordServer()->add($data);
		}
		$bookCourseInfo = HttpDaoManager::getBookCourseDao()->get($_POST['book_course_id']);
		$url='BookCourse_lists.html' ; 
		
		if($_SESSION[ 'admin']['teacher_id']>0){ 
			$url = 'BookCourse_teacherGetList.html'; 
		}
		if($bookCourseInfo['class_status']==CLASS_STATUS_1||$bookCourseInfo['class_status']==CLASS_STATUS_0){//未完成状态可以更新
			$courseRecord['user_status'] = $_POST['user_status'];//若是学生缺课，给一半工资
			HttpServerManager::getBookCourseServer()->haveCompleteClass($courseRecord,$_POST['book_course_id']);
			HttpServerManager::getBookCourseServer()->complete($_POST['book_course_id']);//	老师直接完成预约单
			ClassManager::getToos()->msgbox(Common::_t('data_edit_success'),$url);
		}else{
			ClassManager::getToos()->msgbox(Common::_t('data_operation_invalid'),$url);
		}
		
	}
	
	public function getNowTime(){
		$res = array();
		$res['success'] ='success';
		$res['value'] =time();
		exit(json_encode($res));
	}
}
?>